<template>
  <div class="goodswrap">
    <div class="container">
      <div class="bread-crumbs">
        <a @click="router.push('/')" class="bread-crumbs-tohome">首页</a>
        <span class="goods-icon">></span>
        <span class="goods-bread-name">{{ cateInfo && cateInfo.name }}</span>
      </div>
      <div class="goods-area">
        <div class="categroy">
          <span class="name">分类：</span>
          <div class="categroy-group" @click="searchBysubCate">
            <a
              :data-id="route.query.categoryId"
              :style="{
                color:
                  route.query.categoryId == route.query.subCategoryId
                    ? '#D6BB94'
                    : '#333',
              }"
              >全部</a
            >
            <span v-if="subValueList.length">
              <a
                v-for="cate in subValueList"
                :data-id="cate.id"
                :key="cate.id"
                :style="{
                  color:
                    cate.id == route.query.subCategoryId ? '#D6BB94' : '#333',
                }"
                >{{ cate.name }}</a
              >
            </span>
          </div>
        </div>
        <div class="area">
          <span class="name">配送地区：</span>
          <div class="categroy-group" @click="searchBysubCate">
            <a
              v-for="(areaTitle, index) in area"
              :data-deliveryAreaId="index"
              :style="{
                color:
                  `${index}` == route.query.deliveryAreaId ? '#D6BB94' : '#333',
              }"
              >{{ areaTitle }}</a
            >
          </div>
        </div>
        <div class="sorts" @click="searchBysubCate">
          <span class="name">排序：</span>
          <a
            v-for="(st, index) in sortType"
            :data-sort-type="index"
            :style="{
              color: `${index}` == route.query.sortType ? '#D6BB94' : '#333',
            }"
            :data-desc-sorted="!(route.query.descSorted == 'true')"
            >{{
              index > 0
                ? st + (`${index}` == route.query.sortType ? descSorted : "")
                : st
            }}</a
          >
        </div>
        <div class="content">
          <div>
            <WLoading v-show="!itemList" />
            <ul class="item-list" v-to-detail="router" v-if="itemList">
              <li v-for="item in itemList" :key="item.id">
                <a class="item-list-img">
                  <div class="item-list-div">
                    <img :src="item.primaryPicUrl" :data-id="item.id"/>
                    <img class="item-list-div-img2" :src="item.scenePicUrl"/>
                  </div>
                </a>
                <div class="bd">
                  <h4 class="bd-h4">
                    <span><a :data-id="item.id">{{ item.name }}</a></span>
                  </h4>
                  <p class="bd-p">
                    <span>¥{{ item.retailPrice }}</span>
                  </p>
                  <hr class="hr" />
                  <div>
                    <p class="desc">{{ item.simpleDesc }}</p>
                  </div>
                </div>
                <div class="colorNum" v-if="item.productPlace">
                  <span>{{ item.productPlace }}</span>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
  export default {
    name: "GoodList",
  };
</script>

<script lang="ts" setup>
  import { ref, watch, computed } from "vue";
  import { useRouter, useRoute } from "vue-router";
  import { useHeaderStore } from "@/stores/modules/header";
  import { reqGoodsListApi } from "@/api/goodslist/index";
  import {useGoodListStore} from '@/stores/modules/goodList'
  import { storeToRefs } from "pinia";
import WLoading from "@/components/WLoading/index.vue";
  const router = useRouter();
  const route = useRoute();
  const area = ["全部", "香港", "澳门", "台湾"];
  const sortType = ["默认", "价格", "上架时间"];
  const goodListStore = useGoodListStore()
  const {getGoodList} = goodListStore
  const descSorted = computed(() => {
    return route.query.descSorted == "true" ? "⬇" : "⬆";
  });
  //获取分类数据
  const subValueList = computed<any[]>(() => {
    let cate = cateList.value.filter(
      (cateItem) => cateItem.id == Number(categoryId.value)
    )[0];
    return cate
      ? cate.subCateGroupList.reduce((prev, curr) => {
          return prev.concat(curr.categoryList as any);
        }, [])
      : [];
  });
  //面包屑
  const cateInfo = computed(() => {
    return cateList.value.filter(
      (cateItem) => cateItem.id == Number(categoryId.value)
    )[0];
  });
  const categoryId = ref<string>("");
  const { cateList } = storeToRefs(useHeaderStore());

  const itemList = ref<any>();

  categoryId.value = route.query.categoryId as string;
  //条件查询
  const searchBysubCate = (e) => {
    e.target.nodeName === "A" &&
      router.push({
        name: "WGoodsList",
        query: {
          categoryId: categoryId.value,
          subCategoryId: +e.target.dataset.id || route.query.subCategoryId,
          deliveryAreaId:
            +e.target.dataset.deliveryareaid ||
            (e.target.dataset.deliveryareaid === "0" && !"0") ||
            route.query.deliveryareaid ||
            0,
          sortType:
            e.target.dataset.sortType ||
            (e.target.dataset.sortType === "0" && !"0") ||
            route.query.sortType ||
            0,
          descSorted:
            (route.query.sortType == undefined && "true") ||
            (e.target.dataset.sortType !== route.query.sortType && "true") ||
            `${e.target.dataset.descSorted}` ||
            route.query.descSorted ||
            "true",
        },
      });
  };
  // 发请求
  const getItemList = async () => {
    itemList.value = null;
    const result = await reqGoodsListApi({
      categoryId: categoryId.value as any,
      sortType: route.query.sortType as any,
      descSorted: route.query.descSorted as any,
      subCategoryId: route.query.subCategoryId as any,
      deliveryAreaId: route.query.deliveryAreaId as any,
    });
    getGoodList({
      categoryId: categoryId.value as any,
      sortType: route.query.sortType as any,
      descSorted: route.query.descSorted as any,
      subCategoryId: route.query.subCategoryId as any,
      deliveryAreaId: route.query.deliveryAreaId as any,
    })
    itemList.value = result.itemList.filter((item, index) => {
      return index <= 50;
    });
  };
  watch(
    () => route.query,
    () => {
      categoryId.value = route.query.categoryId as string;
      getItemList();
    },
    {
      immediate: true,
    }
  );
</script>

<style scoped lang="less">
  .goodswrap {
    background-color: #f5f5f5;
    padding-bottom: 80px;
  }
  .container {
    width: 1090px;
    margin: 0 auto;
    box-sizing: border-box;
  }
  .bread-crumbs {
    padding: 20px 0;
    font-size: 12px;
  }
  .bread-crumbs .goods-icon {
    margin: 0 5px;
  }
  .bread-crumbs a {
    color: #333;
  }
  .goods-area {
    background-color: #fff;
    padding: 0 40px;
  }
  .goods-bread-name {
    color: #999;
  }
  .categroy {
    padding-top: 20px;
    display: flex;
    border-bottom: 1px dashed #e1e1e1;
  }
  .name {
    margin-right: 28px;
    color: #999;
    font-size: 14px;
  }
  .categroy-group {
    width: 930px;
    margin-bottom: 4px;
    overflow: hidden;
  }
  .categroy-group a {
    float: left;
    margin-right: 30px;
    margin-bottom: 16px;
    height: 14px;
    line-height: 14px;
    font-size: 14px;
    color: #333;
  }
  .categroy-group a:hover {
    cursor: pointer;
    color: #b4a078;
  }
  .area {
    padding-top: 20px;
    display: flex;
    border-bottom: 1px dashed #e1e1e1;
  }
  .area .name {
    margin-right: 0;
  }
  .sorts {
    padding: 20px 0;
    border-bottom: 1px solid #e1e1e1;
  }
  .sorts a {
    margin-right: 30px;
    font-size: 14px;
    color: #333;
  }
  .sorts a:hover {
    cursor: pointer;
    color: #b4a078;
  }
  .item-list {
    margin-top: 20px;
    display: flex;
    flex-wrap: wrap;
    width: 1020px;
  }
  .item-list li {
    margin-right: 10px;
    margin-bottom: 50px;
    height: 401px;
    position: relative;
    img{
      cursor: pointer;
    }
  }
  .item-list li:hover .item-list-div-img2 {
    opacity: 0;
    display: none;
    z-index: -9999;
  }
  .item-list li:hover .colorNum {
    opacity: 0;
  }
  .item-list li:hover {
    background-color: #f4f0ea;
    border-radius: 8px;
    box-shadow: 3px 3px 8px rgb(203, 193, 193), -3px -3px 8px rgb(203, 193, 193);
  }
  ul li .bd h4 {
    margin: 23px 39.5px 4px 39.5px;
    width: 166px;
    text-align: center;
  }
  .bd-h4  span {
    font-weight: bold;
    line-height: 20px;
    color: #333;
    font-size: 13px;
    a:hover{
      color:#a7936e;
      cursor: pointer;
    }
  }
  .bd-p {
    text-align: center;
    font-size: 13px;
    color: #d4282d;
    height: 21.5px;
  }
  .bd-p span {
    line-height: 20px;
  }
  .hr {
    display: block;
    width: 130px;
    padding: 0;
    margin: 6px auto 14px;
    border: none;
    border-top: 1px solid #e8e8e8;
  }
  .bd div p {
    line-height: 18px;
    box-sizing: border-box;
    color: #999;
    width: 210px;
    overflow: hidden;
    white-space: nowrap;
    text-align: center;
    font-size: 13px;
    text-overflow: ellipsis;
    margin: 0 auto;
  }
  .item-list-div {
    height: 245px;
    position: relative;
  }
  .item-list-div img {
    position: absolute;
    left: 0;
    width: 245px;
    height: 245px;
  }
  .bd-h4:hover span {
    color: #b4a078;
  }
  .hd {
    width: 100%;
    height: 76.5px;
    margin-bottom: 40px;
    padding-top: 30px;
  }
  .hd-p1 {
    text-align: center;
    margin-bottom: 10px;
  }
  .hd-p1 span {
    font-size: 24px;
    font-weight: 700;
    line-height: 44px;
  }
  .hd-p2 {
    text-align: center;
    font-size: 14px;
    color: #999;
    line-height: 1.5;
  }
  .colorNum {
    position: absolute;
    top: 10px;
    left: 10px;
    height: 20px;
    font-size: 12px;
    color: #a7936e;
    border: 1px solid #b3a07d;
    border-radius: 5px;
    padding: 0 4px;
    line-height: 18px;
    background-color: #f4f0ea;
  }
  .bread-crumbs-tohome {
    cursor: pointer;
  }
  .active {
    color: "#D6BB94";
  }
</style>
